'''
    Goblinoid: Experience all of MediaGoblin on an Android Device
    Copyright (C) 2015 Dylan Jeffers

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

from __future__ import print_function

from kivy.app import App
from kivy.properties import ObjectProperty, StringProperty, ListProperty
from kivy.lang import Builder
from kivy.uix.screenmanager import SlideTransition
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.actionbar import ActionBar
from kivy.uix.togglebutton import ToggleButton
from kivy.graphics import Color, Rectangle
from kivy.animation import Animation
from kivy import utils

from goblinoid.services.login_service import remove_credential
from goblinoid.root.goblinscreen import GoblinScreen
from goblinoid.root.goblinscreenmanager import GoblinScreenManager

Builder.load_file('goblinoid/root/actionview.kv')


class OptionsButton(ToggleButton):
    state_normal_source = StringProperty()
    state_down_source = StringProperty()

    def on_state(self, instance, state):
        if state == 'normal':
            self.ids.icon.source = self.state_normal_source
        else:
            self.ids.icon.source = self.state_down_source


class OptionsBar(AnchorLayout):
    pressed = StringProperty()


class GoblinBar(ActionBar):

    def log_out(self):
        print('logging out...')
        remove_credential(App.get_running_app().pump_service.data_dir)
        App.get_running_app().open_view('login', True)


class ActionLayout(GoblinScreen):
    asm = ObjectProperty(GoblinScreenManager())
    options_bar = ObjectProperty()
    goblin_bar_animation = ObjectProperty()
    max_color = ListProperty(utils.get_color_from_hex('#86D4B1'))
    min_color = ListProperty(utils.get_color_from_hex("#2a4237"))
    base_color = ListProperty([0, 0, 0, .1])
    error_color = ListProperty(utils.get_color_from_hex("#ef4832"))

    def __init__(self, **kwargs):
        super(ActionLayout, self).__init__(**kwargs)
        self.transition = SlideTransition(duration=.35)
        self.asm.transition = self.transition
        # note: currently using pump model, but will eventually have its
        # own model
        self.asm.pump_service = App.get_running_app().pump_service
        self.open_action_view('feed')

    def open_action_view(self, name):
        # for the times we manually call this function:
        self.toggle_button(name)
        print('got to open_view: {0}'.format(name))
        # if self.asm.has_screen(name):
        if name == 'upload' and self.asm.has_screen(name):
            self.asm.remove_widget(self.asm.get_screen(name))
        if not self.asm.has_screen(name):
            screen = self.asm.pump_service.screen_dict[name](name=name)
            self.asm.add_widget(screen)
        # grabs screen from dictionary of screens
        if name == 'feed':
            self.transition.direction = 'right'
        else:
            self.transition.direction = 'left'
        self.asm.current = name

    # TODO figure out a better way to open a view
    def toggle_button(self, label_name):
        options_bar = self.ids['options_bar']
        options_bar.pressed = label_name
        for key in options_bar.ids:
            options_bar.ids[key].state = 'normal'
        options_bar.ids[label_name].state = 'down'

    def on_comment(self, *largs):
        self.asm.current_screen.dispatch('on_comment', *largs)
        return True

    def on_feed_refreshing(self, *largs):
        print ('got to feed refreshing dispatch')
        self.goblin_bar_animation = Animation(color=self.max_color, duration=1) + Animation(color=self.min_color, duration=1)
        self.goblin_bar_animation.repeat = True
        self.goblin_bar_animation.start(self.ids.goblin_bar)
        return True

    def on_feed_refreshed(self, *largs):
        print ('got to feed refreshed dispatch')
        self.goblin_bar_animation.repeat = False
        self.goblin_bar_animation.stop_all(self.ids.goblin_bar)
        end_anim = Animation(color=self.base_color)
        end_anim.start(self.ids.goblin_bar)
        return True

    def on_pull_to_refresh(self, *largs):
        percent_from_full_pull = largs[0]
        max_base_color_diff = [self.max_color[i] - self.base_color[i] for i in range(len(self.max_color))]
        added_color = [max_base_color_diff[i] * percent_from_full_pull for i in range(len(max_base_color_diff))]
        print (self.base_color, self.max_color)
        print (percent_from_full_pull, added_color)
        self.ids.goblin_bar.color = [self.base_color[i] + added_color[i] for i in range(len(added_color))]

    def on_no_service(self, *largs):
        self.goblin_bar_animation.repeat = False
        self.goblin_bar_animation.stop_all(self.ids.goblin_bar)
        error_anim = Animation(color=self.error_color)
        error_anim.start(self.ids.goblin_bar)
